<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 分类下拉框（搜索） -->
      <el-form-item label="分类" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择分类" clearable>
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:news:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:news:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:news:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:news:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="newsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="缩略图" align="center" prop="thumbnailUrl" width="100">
        <template slot-scope="scope">
          <el-image 
            v-if="scope.row.thumbnailUrl"
            :src="getFullImageUrl(scope.row.thumbnailUrl)" 
            style="width: 50px; height: 50px;"
            :preview-src-list="[getFullImageUrl(scope.row.thumbnailUrl)]">
          </el-image>
          <span v-else>暂无图片</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="描述" align="center" prop="desc" />
      <!-- <el-table-column class="content-column" label="内容" align="center" prop="content" /> -->
      <el-table-column label="链接地址" align="center" prop="linkUrl" />
      <!-- 分类显示为名称 -->
      <el-table-column label="分类" align="center" prop="typeId">
        <template slot-scope="scope">
          <span>
            {{ getTypeName(scope.row.typeId) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:news:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:news:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listNews, delNews } from "@/api/system/news"
// 新增：引入分类接口
import { listNews_type } from "@/api/system/news_type"
import { BASE_FILE_URL, BASE_API } from '@/utils/constants'

export default {
  name: "News",
  data() {
    return {
      // 遮罩层
      loading: true,
      BASE_API: BASE_API, // 添加BASE_API到data中
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业资讯表格数据
      newsList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        thumbnailUrl: null,
        title: null,
        desc: null,
        content: null,
        linkUrl: null,
        typeId: null,
      },
      // 分类选项
      typeOptions: [],
    }
  },
  created() {
    this.getList()
    this.getTypeOptions()
  },
  methods: {
    /** 获取分类选项 */
    getTypeOptions() {
      listNews_type().then(res => {
        // 兼容接口返回格式
        if (res && (res.data || res.rows)) {
          this.typeOptions = res.data || res.rows
        } else if (Array.isArray(res)) {
          this.typeOptions = res
        } else {
          this.typeOptions = []
        }
      })
    },
    /** 根据typeId获取分类名称 */
    getTypeName(typeId) {
      const found = this.typeOptions.find(item => item.id == typeId)
      return found ? found.name : typeId
    },
    /** 拼接服务器图片地址 */
    getFullImageUrl(imageUrl) {
      if (!imageUrl) return '';
      // 如果已经是http/https/base64开头，直接返回
      if (/^(http|https):\/\//.test(imageUrl) || imageUrl.startsWith('data:image/')) {
        return imageUrl;
      }
      // 兼容斜杠
      if (imageUrl.startsWith('/')) {
        return this.BASE_API + imageUrl;
      }
      // 默认拼接文件服务器路径
      return BASE_FILE_URL + imageUrl;
    },
    /** 查询企业资讯列表 */
    getList() {
      this.loading = true
      listNews(this.queryParams).then(response => {
        this.newsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/system/news/addNews')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push(`/system/news/addNews?id=${row.id}`)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除企业资讯编号为"' + ids + '"的数据项？').then(function() {
        return delNews(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/news/export', {
        ...this.queryParams
      }, `news_${new Date().getTime()}.xlsx`)
    },

  }
}
</script>
<style lang="scss" scoped>
.el-table{
  .el-table__cell{
    padding: 0px !important;
  }
}

.content-column {
  max-height: 200px !important;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: pre-wrap;
}
</style>


